//
//  KDDCollectionWrapper.m
//  MengLiao
//
//  Created by xiuxin on 2022/7/15.
//  Copyright © 2022 ZHBen. All rights reserved.
//
#import "KDDCollectionWrapper.h"
#import "KDDConfigStore.h"
#import "KMContext.h"
#import "KDDCollectionAttributes.h"
#import "KMConfigStore.h"

@implementation KDDCollectionHeaderFooterView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        _contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        [self addSubview:self.contentView];
        [_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(@(0)).priority(MASLayoutPriorityRequired);
        }];
    }
    return self;
}

- (BOOL)respondsToSelector:(SEL)aSelector
{
    return [super respondsToSelector:aSelector] || [self.kmView respondsToSelector:aSelector];
}

- (void)forwardInvocation:(NSInvocation *)anInvocation
{
    if ([super respondsToSelector:anInvocation.selector])
    {
        [super forwardInvocation:anInvocation];
    }
    else if ([self.kmView respondsToSelector:anInvocation.selector])
    {
        [anInvocation invokeWithTarget:self.kmView];
    }
}

- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector
{
    NSMethodSignature * sig = nil;
    if ((sig = [super methodSignatureForSelector:aSelector]))
    {
        return sig;
    }
    return [self.kmView methodSignatureForSelector:aSelector];
}

- (void)prepareForReuse
{
    [super prepareForReuse];
    if ([self.kmView respondsToSelector:_cmd])
    {
        [self.kmView prepareForReuse];
    }
}

- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    self.attributes = layoutAttributes;
    [super applyLayoutAttributes:layoutAttributes];
    self.layer.zPosition = layoutAttributes.zIndex;
    if ([self.kmView respondsToSelector:_cmd])
    {
        [self.kmView applyLayoutAttributes:layoutAttributes];
    }
}

@end

@implementation KDDCollectionCell

- (BOOL)respondsToSelector:(SEL)aSelector
{
    return [super respondsToSelector:aSelector] || [self.kmView respondsToSelector:aSelector];
}

- (void)forwardInvocation:(NSInvocation *)anInvocation
{
    if ([super respondsToSelector:anInvocation.selector])
    {
        [super forwardInvocation:anInvocation];
    }
    else if ([self.kmView respondsToSelector:anInvocation.selector])
    {
        [anInvocation invokeWithTarget:self.kmView];
    }
}

- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector
{
    NSMethodSignature * sig = nil;
    if ((sig = [super methodSignatureForSelector:aSelector]))
    {
        return sig;
    }
    return [self.kmView methodSignatureForSelector:aSelector];
}

- (void)prepareForReuse
{
    if ([self respondsToSelector:@selector(prepareForReuse)]) {
        [super prepareForReuse];
    }
    if ([self.kmView respondsToSelector:_cmd])
    {
        [self.kmView prepareForReuse];
    }
}

- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    self.attributes = layoutAttributes;
    [super applyLayoutAttributes:layoutAttributes];
    if ([self.kmView respondsToSelector:_cmd])
    {
        [self.kmView applyLayoutAttributes:layoutAttributes];
    }
}

@end

@implementation KDDCollectionDecorationView
@dynamic contentView;

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (UIView *)contentView
{
    return self;
}

- (void)applyLayoutAttributes:(KDDCollectionDecorationAttributes *)layoutAttributes
{
    self.attributes = layoutAttributes;
    layoutAttributes.decorationModel.decorationView = self;
    
    [super applyLayoutAttributes:layoutAttributes];
    //iOS10 zindex随机失效修复
    self.layer.zPosition = layoutAttributes.zIndex;
    self.userInteractionEnabled = NO;
    if (self.kmView == nil)
    {
        NSArray<NSString *> * components = [self.reuseIdentifier componentsSeparatedByString:@"__"];
        NSString * viewID = components.firstObject;
        NSString * spec = components.count > 1 ? components[1] : nil;
        Class<KMDecorationView> decorationViewClass = [KMConfigStore decorationForViewID:viewID];
        [(id)decorationViewClass addToWrapper:self frame:layoutAttributes.frame spec:spec];
    }
    if ([self.kmView respondsToSelector:@selector(kmLoadModelList:position:)])
    {
        [(UIView<KMDecorationView> *)self.kmView kmLoadModelList:layoutAttributes.decorationModel.modelList position:layoutAttributes.decorationModel.position];
    }
    if ([self.kmView respondsToSelector:_cmd])
    {
        [self.kmView applyLayoutAttributes:layoutAttributes];
    }
}

- (void)prepareForReuse
{
    [super prepareForReuse];
    if ([self.kmView respondsToSelector:_cmd])
    {
        [self.kmView prepareForReuse];
    }
}

@end
